/*
 * ============================================================================
 * ============================================================================
 */

#ifndef SMS_ENGINE
#define SMS_ENGINE

// INCLUDES
#include <e32base.h> // CBase
#include <msvapi.h>  // MMsvSessionObserver
#include <badesca.h> // CDesCArrayFlat
#include <msvids.h>

// FORWARD DECLARATIONS
class CClientMtmRegistry;
class CSmsClientMtm;
class MSMSMtmsEngineObserver;

// CLASS DECLARATION

/**
 *  SMS engine.
 */
class CSMSMtmsEngine : public CActive, public MMsvSessionObserver
{
public:

    /**
     * Create new CSMSMtmsEngine object
     *
     * @param aObserver a reference to MSMSMtmsEngineObserver.
     * @return a pointer to the created instance of CSMSMtmsEngine
     */
    static CSMSMtmsEngine* NewL( MSMSMtmsEngineObserver& aObserver );

    /**
     * Destructor.
     */
    ~CSMSMtmsEngine( );

public:

    /**
     * Send an SMS that has been created with CreateSMSMessageL().
     */
    void SendSMSL( );

    /**
     * Create an SMS Message.
     * @param aAddress a destination address.
     * @param aMessage Message to be sent.
     */
    TMsvId CreateSMSMessageL( const TDesC& aAddress, const TDesC& aMessage, TMsvId folderid = KMsvDraftEntryId );

    /**
     * Validate Message that has been created with CreateDraftSMSMessageL().
     */
    TBool ValidateCreatedSMS( );

    /**
     * Move an SMS Message to folder.
     * @param aMessageID id of an SMS Message.
     * @param aFolder target folder id.
     */
    void MoveToFolderL( TMsvId aMessageId, TMsvId aFolder );

    /**
     * Delete an SMS Message.
     * @param aMessageID id of an SMS Message.
     */
    void DeleteMessageL( TMsvId aMessageId );

    /**
     * Copy an SMS Message to folder.
     * @param aMessageID id of an SMS Message.
     * @param aFolder target folder id.
     */
    void CopyMessageL( TMsvId aMessageId, TMsvId aFolder );

    /**
     * Gets the originator address.
     * @param aMessageID id of an SMS Message.
     * @param aAddress originator address.
     */
    void GetMessageAddressL( TMsvId aMessageId, TDes& aAddress );

    /**
     * Gets the Message body from the mfging store.
     * @param aMessageID id of an SMS Message.
     * @param aMessage the Message.
     * @return ETrue if Message correspongin aMessageId is valid SMS
     *         (.sis files / settings are filtered out), EFalse otherwise.
     */
    TBool GetMessageL( TMsvId aMessageId, TDes& aMessage );

    /**
     * Gets the Message body summary information(just the beginning of body)
     * @param aMessageID id of an SMS Message.
     * @param aMessage the beginning of Message.
     * @return ETrue if Message correspongin aMessageId is valid SMS
     *         (.sis files / settings are filtered out), EFalse otherwise.
     */
    TBool GetMessageIndexBodyTextL( TMsvId aMessageId, TDes& aMessage );

    /**
     * Retrieve one folder's SMS Messages (their address+Message body).
     * @param aAddresses Array of recipient addresses
     * @param aMessages Array of Message bodys.
     */
    void GetFolderSMSMessageInformationL( TMsvId aFolderID, CDesCArrayFlat*& aAddresses, CDesCArrayFlat*& aMessages );

    /**
     * All ids of Messages that has been got with
     * GetFolderSMSMessageInformationL.
     * @return array of ids.
     */
    RArray< TMsvId >* GetMessageIds( );

    TBool IsSmsExist( TMsvId itemid );
    TBool setSmsItemContent( const TDesC& aAddr, const TDesC&aMsg, TMsvId itemid );
private:
    /**
     * A constructor.
     * @param aObserver Observer of this engine.
     */
    CSMSMtmsEngine( MSMSMtmsEngineObserver& aObserver );

    /**
     * ConstructL()
     */
    void ConstructL( );

    /**
     * Creates SMS client.
     */
    void CreateMtmClientL( );

private:
    // from CActive
    virtual void DoCancel( );
    virtual void RunL( );

private:
    // from MMsvSessionObserver
    void HandleSessionEventL( TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* aArg3 );

private:

    // SMS client MTM interface
    CSmsClientMtm* iSmsMtm;

    // Observer of this engine.
    MSMSMtmsEngineObserver& iObserver;

    // Controls asynchronous commands in the mfging system
    // Get progress information about the sending.
    CMsvOperation* iMsvOper;

    // Encapsulates an array of entry IDs.
    CMsvEntrySelection* iEntrySelection;

    // Represents a session to the mfging server
    CMsvSession* iSession;

    // This registry holds details of the all the Client-side
    // MTMs currently available on the system.
    CClientMtmRegistry* iClientMtmReg;

    // Array of ids
    RArray< TMsvId >* iIdArray;

    // Id of an SMS Message
    TMsvId iSmsId;
};

/**
 *  CSMSMtmsEngine SMS engine observer.
 */
class MSMSMtmsEngineObserver
{
public:
    virtual void HandleMessageSentL( TInt aError ) = 0;
};

#endif  // SMS_EXAMPLE_ENGINE
